package com.rj.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rj.dao.UrlJsonDao;
import com.rj.dao.impl.UrlJsonDaoImpl;
import com.rj.service.UrlJsonService;
import com.rj.service.impl.UrlJsonServiceImpl;

public class UrlJsonServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@SuppressWarnings("deprecation")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String tempName = request.getParameter("cityName");
		String cityName = URLDecoder.decode(tempName, "utf-8");
		String cityFilePath = request.getRealPath("config/cwc.txt");

		System.out.println("cityName====" + cityName);

		UrlJsonService urlJService = new UrlJsonServiceImpl();
		String areaJson="notthiscity";
		if (urlJService.getJsonFromOtherPage(cityName, cityFilePath) != null) {
			areaJson = urlJService.getJsonFromOtherPage(cityName,
					cityFilePath);
		}
		System.out.println("areaJson=" + areaJson);

		out.write(areaJson);

		out.flush();
		out.close();
	}

	public void init() throws ServletException {
	}

}
